﻿using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
    public Text timerText;
    private float totalTime = 300f; // 2分钟，共120秒
    public bool isEnd = false;
    private bool isRunning = false;
    private bool isDelayed = true;
    private float delayTime = 3f; // 延迟3秒
    public Text EndText;
    public Text StartText;

    // 引用两个角色的组件
    public PlayerController player1A;
    public PlayerController player2A;

    public Image End;
    public Image StartI;

    public AudioSource I;

    void Start()
    {
        Debug.Log(Global.Player1);
        Debug.Log(Global.Player2);
        if (timerText == null)
        {
            GameObject OtimerText = GameObject.Find("计时器");
            timerText = OtimerText.GetComponent<Text>();
        }
        
        GameObject End1 = GameObject.Find("时间结束");
        End = End1.GetComponent<Image>();

        GameObject EndT = GameObject.Find("结束文字");
        EndText = EndT.GetComponent<Text>();

        GameObject StartT = GameObject.Find("时间开始");
        StartI = StartT.GetComponent<Image>();

        GameObject StartTextT = GameObject.Find("开始文字");
        StartText = StartTextT.GetComponent<Text>();

        I = gameObject.GetComponent<AudioSource>();
        I.volume = PlayerPrefs.GetFloat("Volume", 1f);
        // 查找并获取两个角色的引用
        FindPlayerCharacters();

        UpdateTimerText();
    }

    // 查找并缓存两个角色的引用
    private void FindPlayerCharacters()
    {
        // 查找Player1角色
        GameObject player1Object = GameObject.FindGameObjectWithTag("Player1");
        if (player1Object != null)
        {
            player1A = player1Object.GetComponent<PlayerController>();
            if (player1A == null)
            {
                Debug.LogError("在'Player1'上未找到Player1A组件！");
            }
        }
        else
        {
            Debug.LogError("未找到名为'Player1'的游戏对象！请检查名称是否正确");
        }

        // 查找Player2角色
        GameObject player2Object = GameObject.FindGameObjectWithTag("Player2");
        if (player2Object != null)
        {
            player2A = player2Object.GetComponent<PlayerController>();
            if (player2A == null)
            {
                Debug.LogError("在Player2对象上未找到Player2A组件！");
            }
        }
        else
        {
            Debug.LogError("未找到名为'Player2'游戏对象！请检查名称是否正确");
        }
    }

    void Update()
    {
        if (isDelayed)
        {
            delayTime -= Time.deltaTime;
            if (delayTime <= 0)
            {
                I.Play();
                isDelayed = false;
                isRunning = true;
                Animation animation = StartI.GetComponent<Animation>();
                animation.Play("TimeIsBegin");
                // 延迟结束时重置两个角色的状态
                ResetPlayerStates();
            }
            UpdateDelayText();
        }
        else if (isRunning)
        {
            totalTime -= Time.deltaTime;
            if (totalTime <= 0 || player1A.health == 0 || player2A.health == 0)
            {
                totalTime = 0;
                isRunning = false;
                isEnd = true;
                if (totalTime <= 0)
                    EndText.text = "Time is up";
                if (player1A.health == 0 || player2A.health == 0)
                    EndText.text = "Game Over";

                Animation animation = End.GetComponent<Animation>();
                animation.Play("TimeIsOver");
                player1A.isReady = false;
                player2A.isReady = false;

            }
            UpdateTimerText();
        }
    }

    // 重置两个角色的状态
    private void ResetPlayerStates()
    {
        if (player1A != null)
        {
            player1A.isReady = true;
            Debug.Log("已重置Player1A的isKnockedBack状态");
        }

        if (player2A != null)
        {
            player2A.isReady = true;
            Debug.Log("已重置Player2A的isKnockedBack状态");
        }
    }

    private void UpdateTimerText()
    {
        int minutes = Mathf.FloorToInt(totalTime / 60f);
        int seconds = Mathf.FloorToInt(totalTime % 60f);
        timerText.text = string.Format("{0:00}:{1:00}", minutes, seconds);
    }

    private void UpdateDelayText()
    {
        timerText.text = string.Format("Ready: {0:0}", Mathf.Ceil(delayTime));
    }
}